package com.home.cop.sysinfo.controller;

import com.home.cop.base.BaseController;
import com.home.cop.sysinfo.model.SysDataDict;
import com.home.cop.sysinfo.service.SysDataDictService;
import com.home.cop.system.RequiredAuth;
import com.home.cop.system.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/dict")
@Api("数据字典相关")
public class SysDataDictController extends BaseController {

    @Autowired
    private SysDataDictService dictService;

    @RequiredAuth
    @PostMapping("/list")
    @ApiOperation(value = "数据字典列表", notes = "根据编号获取当前分组的数据字典")
    @ApiImplicitParams({ @ApiImplicitParam(name = "code", value = "编号，多个用逗号隔开", required = false, dataType = "String", paramType = "query")
    })
    public Result list(@RequestParam(value = "code",required = false) String code){
        Result result = Result.returnSuccess();
        String[] codes = code.split(",");
        for (String codeStr:codes){
            result.pushData(codeStr,dictService.list(codeStr));
        }
        return result;
    }

    @RequiredAuth
    @PostMapping("/tree")
    @ApiOperation(value = "数据字典列表", notes = "按照树形结构获取当前分组的数据字典")
    public Result listTree(){
        return Result.returnSuccess().setData(dictService.listTree());
    }

    @RequiredAuth
    @PostMapping("/add")
    @ApiOperation(value = "新增数据字典", notes = "传入json格式新增数据字典")
    public Result addSysDict(@RequestBody SysDataDict sysDataDict){
        dictService.insert(sysDataDict);
        return Result.returnSuccess().setMessage("新增成功！");
    }

    @RequiredAuth
    @PostMapping("/delete")
    @ApiOperation(value = "删除数据字典", notes = "根据编号删除数据字典")
    @ApiImplicitParams({ @ApiImplicitParam(name = "code", value = "编号", required = true, dataType = "String", paramType = "query")
    })
    public Result deleteDict(@RequestParam("code")String code){
        dictService.delete(code);
        return Result.returnSuccess().setMessage("删除成功！");
    }

    @RequiredAuth
    @PostMapping("/status")
    @ApiOperation(value = "修改数据字典的状态", notes = "修改数据字典的状态")
    @ApiImplicitParams({ @ApiImplicitParam(name = "code", value = "编号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态：0-启动，1-禁用", required = true, dataType = "Integer", paramType = "query")
    })
    public Result changeStatus(@RequestParam("code") String code,@RequestParam("status") Integer status){
        dictService.changeStatus(code,status);
        return Result.returnSuccess().setMessage("操作成功！");
    }
    @RequiredAuth
    @PostMapping("/info")
    @ApiOperation(value = "查看数据字典", notes = "根据编号查看数据字典")
    @ApiImplicitParams({ @ApiImplicitParam(name = "code", value = "编号", required = true, dataType = "String", paramType = "query")
    })
    public Result info(@RequestParam("code") String code){
        return Result.returnSuccess().setData(dictService.info(code));
    }
}
